package com.hdu;

import java.text.SimpleDateFormat;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class SystemTimerTest {
    //驱动时间轮向前的线程
    private static final ExecutorService executorService = Executors.newFixedThreadPool(1);
    public static SystemTimer timer = new SystemTimer("test", 1000L, 5, System.currentTimeMillis());


    public static void runTask() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (int i = 0; i < 120000; i += 3000) {
            // 添加任务，每个任务间隔3s
            timer.add(new TimerTask(i) {
                @Override
                public void run() {
                    long l = System.currentTimeMillis();
                    // 转化为 yyyy-MM-dd HH:mm:ss
                    String s = simpleDateFormat.format(l);
                    System.out.println("运行testTask的时间: " + s);
                }
            });
        }
    }

    public static void main(String[] args) throws Exception {

        // 开启驱动时间轮的线程
        executorService.submit(() -> {
            timer.advanceClock();
        });

        runTask();

        Thread.sleep(1000000);
        timer.shutdown();
        executorService.shutdown();
    }
}